package com.zhangmeng.online.exam.ui.components.callBack;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zhangmeng.online.exam.ui.utils.ApiUtils;
import com.zhangmeng.online.exam.ui.api.DataLoad;
import com.zhangmeng.online.exam.ui.api.model.PaperDataLoad;
import com.zhangmeng.online.exam.ui.api.model.QuestionDataLoad;
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;

import java.util.List;
import java.util.Map;

/**
 * @author zm
 * @date 2025/3/19 14:31
 * @version: 1.0
 */
public class PaperViewComponentCallBackImpl implements PaperViewComponentCallBack {
    @Override
    public ApiUtils.DataView loadQuestionData(int pageNum, int pageSize) {
        DataLoad dataLoad = new QuestionDataLoad();
        return dataLoad.loadData(pageNum, pageSize);
    }

    @Override
    public ApiUtils.DataView loadChooseQuestionData(Map<String, Object> context,int pageNum, int pageSize) {
        PaperDataLoad dataLoad = new PaperDataLoad();
        return dataLoad.loadChooseQuestionData(context,pageNum, pageSize);
    }

    @Override
    public ApiUtils.DataView searchQuestionData(String searchText, int pageNum, int pageSize) {
        DataLoad dataLoad = new PaperDataLoad();
        return dataLoad.searchData(Map.of("searchText", searchText), pageNum, pageSize);
    }

    @Override
    public void commitChooseQuestion(Map<String, Object> context, ObservableList<Map<String, SimpleStringProperty>> chooseTableData) {
        String id = context.get("id").toString();
        List<String> ids = chooseTableData.stream().map(chooseTableDatum -> chooseTableDatum.get("id").get()).toList();
        Map<String, Object> map = Map.of("id", id, "ids", JSONObject.toJSONString(ids));
        String result = HttpUtils.POST(ApiUtils.API_URL + "/paper/chooseQuestion/save", map);
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getIntValue("code") == 200) {
            AlertUtils.alert_msg("保存成功！");
        } else {
            AlertUtils.alert_warning("保存失败！");
        }
    }
}
